

from typing import Any, List, Dict


def getClassFields(cls : Any) -> dict:
    full_attrs = vars(cls)
    fields = {k: v for k, v in full_attrs.items() if not k.startswith('__')}
    return fields

from dataclasses import asdict, dataclass

@dataclass
class PackageVars:
    Package      : str = "包名"
    Version      : str = "版本"
    Architecture : str = "架构"
    Size         : str = "大小"
    Filename     : str = "文件名"


class Package:
    Package: str
    Version: str
    Architecture: str
    Size: str
    Filename: str

    # def __init__(self, packageinfo: dict[str, str]):
    #     self.Package      = packageinfo['Package']
    #     self.Version      = packageinfo['Version']
    #     self.Architecture = packageinfo['Architecture']
    #     self.Size         = packageinfo['Size']
    #     self.Filename     = packageinfo['Filename']

    def loads(self, package : str):
        ''''''
        fields = getClassFields(PackageVars).keys()

        package = package.strip()
        lines = package.split('\n')

        keymap = {}
        for line in lines:
            for field in fields:
                if line.startswith(field):
                    keymap[field] = line.split(':')[1].strip()
                    break

        # return Package(**asdict(keymap))
        return keymap

if __name__ == '__main__':
    # class Test:
    #     Field1 = "第一个字段"

    # print(getClassFields(Test))
    
    # {'Field1': '第一个字段'}

    # print(getClassFields(PackageVars))
    # v = {'Package': '包名', 'Version': '版本', 'Architecture': '架构', 'Size': '大小', 'Filename': '文件名'}
    from json import dumps, loads
    s = PackageVars()
    v = asdict(s)
    v = dumps(v, indent=4)
    # p = (
    #     PackageVars(**loads(v))
    # )
    print(v)
